<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
$uniacid=$_SESSION['uniacid'];
if($_RQ['op']=='main'){
	$actid = intval($_RQ['actid']);
	$price = intval($_RQ['price']);
	$num = intval($_RQ['num']);
	$specid = $_RQ['specid'];
	$return['setting']=$this->plugin_setting('basic');
	//下单页
	$item=array();
	$return['post'] = array();
	if($actid){
		$act=pdo_get('jy_sendgoods_act',array('uniacid'=>$uniacid,'id'=>$actid));
		if(!$act){
			exi('指定活动不存在或已删除！','error','goback');
		}
		$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$uniacid,'id'=>$act['goodsid']));
		$images=json_decode($goods['images'],true);
		$act['image']=$images['0'];
		$return['post']['actid'] = $actid;
		$act['goods']=$goods;
		
		$return['post']['total'] = $act['total'];
		$return['post']['price'] = $price;
		$return['post']['num'] = $num;
		$return['post']['fee'] = $price*$num;
	}
	if($specid){
		//规格
		$specids=explode('_',$specid);
		
		//已选规格
		$selespename = '';
		if($specids){
				foreach($specids as $k=>$sp){
					$spec_item=pdo_get('jy_weishop_goods_spec_item',array('id'=>$sp,'uniacid'=>$_SESSION['uniacid']),array('id','title'));
					$selespename .=$spec_item['title'].'，';
				}
		}
	}
	//整理收货地址信息
	$member_address=pdo_getall('core_address',array('mid'=>$_SESSION['uid']));
	if($member_address){
		foreach($member_address as &$address){
			$address['pcd']=json_decode($address['pcd'],true);
			if($address['isdefault']){
				$item['addressid']=$address['id'];
				$item['address']=$address;
				break;
			}
		}
		if(!$item['addressid']){
			$item['addressid']=$member_address['0']['id'];
			$item['address']=$member_address['0'];
		}
	}
	//查询规格对应的optionid
	$optionid=pdo_getcolumn('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goods['id'],'specs'=>$_RQ['specid']),'id');
	//print_r($optionid.'ff');exit;
	$return['item'] = $item;
	$return['act'] = $act;
	$return['post']['optionid'] = $optionid;
	$return['post']['coupon_type'] = 1;
	$return['selespename'] = $selespename;
	exi($return);
}

if($_RQ['op']=='pay'){
	$post=get_postdata();
	if($post['addressid']){
		$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['addressid']));
	}
	$id=intval($post['actid']);
	if(!$id){
		exi('未获取到活动信息！','error');
	}
	$act=pdo_get('jy_sendgoods_act',array('uniacid'=>$uniacid,'id'=>$id));
	//生成订单
	$insert=array(
		'uniacid'=>$uniacid,
		'mid'=>$_SESSION['uid'],
		'actid'=>$id,
		'goodsid' =>$act['goodsid'],
		'orderno' =>get_ordersn('jy_sendgoods_order','orderno'),
		'fee' => $post['fee'],
		'buynum' => $post['num'],
		'optionid' => $post['optionid'],
		'addressid' => $post['addressid'],
		'status' => 0,
		'ispay' => 0,
		'createtime' => time(),
	);
	  pdo_insert('jy_sendgoods_order',$insert);
	   $orderid=pdo_insertid();
	   //支付
	   $params['fee']=$post['fee'];
	    $params['title']='优品订单';
		$params['id']=$orderid;
		$params['plugin']='jy_sendgoods';
		$params['back_url']='jy_sendgoods/create/payresult';
		$payid=cfc('pay')->create_pay_order($params);
		
		$return['payid']=$payid;
		$return['id']=$orderid;
		exi($return);
	
}

if($_RQ['op']=='payresult'){
	
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_sendgoods_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	//减去库存
	//$log=var_export($order,true);
	pdo_update('jy_sendgoods_order',array('ispay'=>1,'status'=>1,'paytime'=>TIMESTAMP),array('id'=>$order['id']));

}

?>